package cn.tedu.travelsystem.travelDownload.controller;

import cn.tedu.travelsystem.travelDownload.service.FileStorageService;
import cn.tedu.travelsystem.travelDownload.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/user/avatar")
@RequiredArgsConstructor
public class UserAvatarController {
    @Autowired
    private final FileStorageService fileStorageService;
    @Autowired
    private final UserService userService;

    @PostMapping("/upload")
    public String uploadAvatar(
            @RequestParam("file") MultipartFile file,
            @RequestParam Integer userId) throws Exception {

        // 1. 存储文件
        String filename = fileStorageService.storeAvatar(file);

        // 2. 更新数据库
        return userService.updateUserAvatar(userId, filename);
    }
}